package com.intelligent.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.intelligent.dto.InventoryDetailsQueryDTO;
import com.intelligent.pojo.InventoryDetails;
import com.intelligent.service.InventoryDetailsService;
import com.intelligent.utils.Result;
import com.intelligent.vo.InventoryDetailsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 库存明细控制器
 */
@Api(tags = "库存明细管理")
@RestController
@RequestMapping("/inventory-details")
public class InventoryDetailsController {

    @Autowired
    private InventoryDetailsService inventoryDetailsService;

    //分页查询
    @ApiOperation("分页查询库存明细")
    @PostMapping("/page")
    public Result<IPage<InventoryDetailsVO>> pageList(@RequestBody InventoryDetailsQueryDTO queryDTO) {
        IPage<InventoryDetailsVO> page = inventoryDetailsService.pageList(queryDTO);
        return Result.success(page);
    }

    //库存明细
    @ApiOperation("获取库存明细详情")
    @GetMapping("/getDetail")
    public Result<InventoryDetails> getDetail(@RequestParam("id") Long id) {
        InventoryDetails details = inventoryDetailsService.getDetailById(id);
        if (details == null) {
            return Result.error("库存明细不存在");
        }
        return Result.success(details);
    }

    //新增
    @ApiOperation("新增库存明细")
    @PostMapping
    public Result<Boolean> add(@RequestBody InventoryDetails details) {
        try {
            boolean success = inventoryDetailsService.add(details);
            return success ? Result.success(true) : Result.error("新增失败");
        } catch (RuntimeException e) {
            return Result.error(e.getMessage());
        }
    }
}
